package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    @RequestMapping
    public String findPage(HttpServletRequest request, Model model){
        Map<String, Object> filters = super.getFilters(request);
        PageInfo<Admin> page = this.adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }
    @GetMapping("/create")
    public String create(){
        return "admin/create";
    }
    @Autowired
    private PasswordEncoder passwordEncoder;
    @PostMapping("/save")
    public String save(Admin admin){
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        this.adminService.insert(admin);
        return "common/successPage";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id,Model model){
        Admin admin = this.adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin){
        this.adminService.update(admin);
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        this.adminService.delete(id);
        return "redirect:/admin";
    }

    //用户头像上传

    @GetMapping("/uploadShow/{id}")
    public String  uploadShow(@PathVariable Long id,Model model){
        model.addAttribute("id",id) ;
        return  "admin/upload";
    }

    @PostMapping("/upload/{id}")

    public   String upload(@PathVariable Long id, MultipartFile file) throws IOException {
        //上传七牛云的图片
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
         //没有扩展名字
        String fileName = UUID.randomUUID().toString();
        fileName+=extName; //32141234aerqreqadfadf2342134.jpg
        QiniuUtil.upload2Qiniu(file.getBytes(),fileName);
        //写数据到数据库中
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://rig7i1y2z.hb-bkt.clouddn.com/"+fileName);
        adminService.update(admin);
        return "common/successPage";
    }

    @Reference
    private RoleService roleService;

    @GetMapping("/assignShow/{adminId}")
    public  String  assignShow(@PathVariable Long adminId,Model model){
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId",adminId);
        return "admin/assignShow";

    }
//    根据用户分配角色
     @PostMapping("/assignRole")
    public  String  assignRole(Long adminId, Long[] roleIds){
        roleService.saveUserRoleRealtionship(adminId,roleIds);
        return "common/successPage";

     }

    /**
     * 获取当前登录信息
     * @return
     */
    @GetMapping("getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); //通过上下文 拿到用户登录的信息

        return authentication.getPrincipal();
    }
}
